#!/usr/bin/env tclsh
# $Id: ipaddr.tcl 9ce65bed8ef4 2012/08/26 02:14:51 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# __version__  = "$Revision: 9ce65bed8ef4 $"
# __author__   = "$Author: pgurumur $"
# __modified__ = "$Date: 2012-08-26 02:14:51Z $"

package require Tk 8
lappend ::auto_path venya
package require netaddr

wm title . "IP Utility"

label .lbl_address -text "IP Address: "
label .lbl_netmask -text "Netmask: "
label .lbl_version -text "IP version: "
label .lbl_integer -text "IP integer: "
label .lbl_size -text "IP/Network size: "
label .lbl_wildcard -text "Wildcard: "
label .lbl_broadcast -text "Broadcast: "
label .lbl_loopback -text "Loopback: "

entry .ent_address -width 40 -textvariable address
entry .ent_netmask -width 10 -textvariable netmask
entry .ent_version -width 5 -textvariable version
entry .ent_integer -width 40 -textvariable integer
entry .ent_size -width 40 -textvariable size
entry .ent_wildcard -width 20 -textvariable wildcard
entry .ent_broadcast -width 20 -textvariable broadcast

frame .loopback -width 20
radiobutton .loopback.yes -text "Yes" -variable loopback -value "Yes" -width 20
radiobutton .loopback.no -text "No"   -variable loopback -value "No" -width 20

button .ent_compute -text "Compute!" -command compute
button .ent_exit -text "Exit" -command exit

grid config .lbl_address -column 0 -row 0 -sticky "e"
grid config .lbl_netmask -column 0 -row 1 -sticky "e"
grid config .lbl_version -column 0 -row 2 -sticky "e"
grid config .lbl_integer -column 0 -row 3 -sticky "e"
grid config .lbl_size -column 0 -row 4 -sticky "e"
grid config .lbl_wildcard -column 0 -row 5 -sticky "e"
grid config .lbl_broadcast -column 0 -row 6 -sticky "e"

grid config .ent_address -column 1 -row 0 -sticky "snew"
grid config .ent_netmask -column 1 -row 1 -sticky "snew"
grid config .ent_version -column 1 -row 2 -sticky "snew"
grid config .ent_integer -column 1 -row 3 -sticky "snew"
grid config .ent_size -column 1 -row 4 -sticky "snew"
grid config .ent_wildcard -column 1 -row 5 -sticky "snew"
grid config .ent_broadcast -column 1 -row 6 -sticky "snew"

grid config .lbl_loopback -column 2 -row 0 -sticky "e"

grid config .loopback -column 3 -row 0 -sticky "e"
# grid config .rb_lono -column 3 -row 1 -sticky "snew"
grid config .ent_compute -column 3 -row 1 -sticky "snew"
grid config .ent_exit -column 3 -row 2 -sticky "snew"

grid columnconfigure . 1 -weight 1
grid columnconfigure . 3 -weight 1

bind .ent_compute <Return> compute
focus .ent_compute
.loopback.no select

proc compute {} {
   global address netmask version integer size wildcard broadcast

   if {[string length $address]} {
      catch {
         netaddr::ip $address
         set version [netaddr::getversion]
         set integer [netaddr::ip2integer]
         set size [netaddr::getsize]

         if {$version == 4} {
            set netmask [netaddr::getmask]
            set wildcard [netaddr::getwildcard]
            set broadcast [netaddr::ntop [netaddr::getbroadcast] $version]
         } elseif {$version == 6} {
            set netmask ""
            set wildcard ""
            set broadcast ""
         }

      } result errorinfo

      set errargs [split $errorinfo " "]
      set code [lindex $errargs 1]
      if {$code != 0} {
         puts "$address: $errorinfo"
      }
   }
}
